﻿#include <qg/qg.h>

using namespace tpl;

int main(void)
{
	k_mesg("RESOURCE CREATE & TEXTURE DRAW TEST\n");

	qgDevice* dev = qgDevice::New("d11", 640, 480, QGSDF_LAYOUT | QGSDF_AUTO);

	if (dev)
	{
		dev->Mount(QG_WORK_PATH);

		qgStub* stub = dev->GetStub();
		qgFreeType* font = qgFreeType::New();

		// 세이더
		qgShader* shd = dev->CreateShader();
		if (shd)
		{
			shd->BindFile(QGSHADER_VS, "z.vs", 0);
			//shd->Unload();
		}

		// 레이아웃
		qgLayoutElement le[2] =
		{
			{QGDLOU_POSITION, 0, QGDLOT_FLOAT4, 0, 0, },
			{QGDLOU_COLOR, 0, QGDLOT_UINTCOLOR, 0, 0, },
		};
		qgLayout* lo = dev->CreateLayout(le);
		dev->SetLayout(lo);

		// 텍스쳐
		qgTexture* tex = dev->CreateTexture("/texture/z.png", QGTEXF_S2D);

		// 다중 선 그리기 재료
		tpl::Rect bound(150, 150, stub->GetSize().x - 150, stub->GetSize().y - 150);
		tpl::Point vx[4] =
		{
			tpl::Point(bound.left, bound.top),
			tpl::Point(bound.right, bound.top),
			tpl::Point(bound.right, bound.bottom),
			tpl::Point(bound.left, bound.bottom),
		};
		tpl::ColorValue vc[] =
		{
			tpl::ColorValue(0xFFFF0000),
			tpl::ColorValue(0xFF00FF00),
			tpl::ColorValue(0xFF0000FF),
			tpl::ColorValue(0xFFFFFFFF),
		};
		float f = 0.0f, dt = 128.0f;

		//
		while (dev->Run())
		{
			float adv = stub->GetAdvance();

			//
			f += dt*adv;
			if (f > 255.0f)
			{
				dt = -128.0f;
				f = 255.0f;
			}
			else if (f < 64.0f)
			{
				dt = 128.0f;
				f = 64.0f;
			}

			kint n = (kint)f;
			vc[0].r = n;
			vc[1].g = n;
			vc[2].b = n;
			vc[3].r = n;
			vc[3].g = n;
			vc[3].b = n;

			//
			dev->Enter();

			dev->DrawRect(tpl::Rect(50, 50, 400, 400), tpl::ColorValues::White, tex);
			dev->DrawPolyLine(4, vx, vc);

			font->WriteFormat(5, 5, 0, "[%.2f]", stub->GetRunFps());

			dev->Leave();
			dev->Flush();
		}

		font->Unload();
		dev->Unload();
	}

	return 0;
}

